<template>
	<view class="lottery" :style="{height:`${device.screenHeight}px`}">
		<x-navigation-bar :isBack="true" bgColor="#E7D2B7" title="大转盘活动"></x-navigation-bar>

		<template v-if="isLoading">
			<x-loading></x-loading>
		</template>
		<template v-else-if="!isLoading && prizeList && prizeList.length>0">
			<!-- 有数据 -->

			<scroll-view scroll-y
				:style="{height:(device.screenHeight-device.navigationBarHeight-device.bottomHeight)+'px;', 'margin-bottom': device.bottomHeight+'px'}">

				<view class="lottery-name">会员惊喜大转盘</view>
				<view class="active-name" v-if="active && active.name.length>0">{{active.name}}</view>

				<view class="my-lottery-record" @click="onMyLotteryRecord">我的中奖记录</view>

				<x-turntable class="turntable" ref="turntableRef" :data="prizeList" prizeNameField="goodsName"
					prizeImageField="picturePath" @start="emitStart" @finish="emitFinish"></x-turntable>

				<view class="active-rule">
					<view class="header">大转盘活动规则</view>
					<view class="body">
						<view class="integral">
							<view class="title">
								<span class="text">我的积分</span>
								<span class="num">{{integralNum}}</span>
							</view>
							<view class="content">当前活动每次抽奖消耗 {{active.times}} 个积分</view>
						</view>

						<view class="time" v-if="active.startTime && active.endTime">
							<view class="title">【活动时间】</view>
							<view class="start-time">{{active.startTime}}</view>
							<view class="end-time">{{active.endTime}}</view>
						</view>
						<view class="object-user">
							<view class="title">【活动对象】</view>
							<view class="content">葡共营小程序全体注册用户</view>
						</view>
						<view class="rule" v-if="active.lotteryRules && active.lotteryRules.length>0">
							<view class="title">【活动规则】</view>
							<view class="content">
								{{active.lotteryRules}}
							</view>
						</view>
					</view>
				</view>
				<view class="bottom-line"></view>
			</scroll-view>
		</template>
		<template v-else>
			<!-- no data -->
			<view class="no-data">敬请期待</view>
		</template>

	</view>

</template>

<script>
	import XNavigationBar from '@/components/navigationBar/xNavigationBar.vue'
	import XTurntable from '@/components/turntable/xTurntable.vue'

	import {
		getInfo,
		lottery
	} from '@/api/turntable.js'
	import config from '@/config';
	import {
		mapState
	} from 'vuex'

	export default {
		components: {
			XNavigationBar,
			XTurntable,
		},
		computed: {
			...mapState(['device', 'userInfo']),
		},
		data() {
			return {
				imageUrl: this.$Config.imageUrl,
				isLoading: true,
				prizeList: [], //奖品信息
				active: null,
				integralNum: this.$store.state.userInfo.integralNum,
			}
		},
		onLoad() {
			console.log(1, this.$store.state.userInfo)
			this.isLoading = true
			getInfo().then(res => {
				this.active = res;
				this.prizeList = res.detailList.map((item, index) => {
					if (item.picturePath?.length > 0) {
						item.picturePath = this.imageUrl + item.picturePath
					}
					return item; // 返回修改后的元素
				});
				console.log("this.prizeList=", this.prizeList)
				this.isLoading = false
			}).catch(err => {
				this.isLoading = false
			})
		},
		mounted() {

		},
		methods: {
			onMyLotteryRecord() {
				this.gotoRecord()
			},
			gotoRecord() {
				uni.navigateTo({
					url: '/packageLottery/pages/lottery/index'
				})
			},
			emitStart() {
				if (this.integralNum < this.active.times) {
					uni.showToast({
						title: "积分不足，不能参与抽奖",
						icon: 'none'
					})
					return;
				}
				lottery(this.active.id).then(res => {
					if (res) {
						var index; //中奖索引值
						this.integralNum = res.times //返回用户积分
						for (var i = 0; i < this.prizeList.length; i++) {
							if (this.prizeList[i].activityGoodsId == res.goodsId) {
								index = i
							}
						}
						console.log("##server prize=", this.prizeList[index], "index=", index)
						this.$refs.turntableRef.rotating(index)
					} else {
						this.$refs.turntableRef.finish()
					}
				}).catch(err => {
					this.$refs.turntableRef.finish()
				})

			},

			emitFinish(prizeIndex) {
				let prize = this.prizeList[prizeIndex]
				console.log("##result prize=", prize, "index=", prizeIndex)
				// setTimeout(() => {
					
				if (prize.goodsType == -1) { 
					// -1：谢谢惠顾
					uni.showModal({
						content: '很遗憾未中奖！',
						showCancel: false,
						title: '提示'
					})
				} else if (prize.goodsType == 1) {
					// 1:实物奖品
					uni.showModal({
						content: '恭喜您抽中' + prize.goodsName + '，(可在 我的-转盘记录 或 我的中奖记录 中点击查看并领取)',
						title: '提示',
						success: (s) => {
							if (s.confirm) {
								this.gotoRecord();
							}
						}
					})
				} else if (prize.goodsType == 2) {
					// 2:非实物奖品（积分)
					uni.showModal({
						content: '恭喜您抽中！' + prize.goodsName + '(积分奖励实时到账,可在 我的-积分商城-积分明细 中查看获奖记录)',
						showCancel: false,
						title: '提示'
					})
				}else if (prize.goodsType == 3) {
					// 3: 代金券
					uni.showModal({
						content: '恭喜您抽中！' + prize.goodsName + '(代金券奖励实时到账,可在 我的-代金劵 中查看获奖记录)',
						showCancel: false,
						title: '提示'
					})
				} else {
					// do nothings 暂不支持
				}


				// }, 5000);

				// uni.showToast({
				// 	title: '中奖产品是第' + prizeIndex + '产品'
				// })
			}
		}
	}
</script>

<style lang="scss" scoped>
	.lottery {
		width: 100%;
		height: 100%;
		background: linear-gradient(180deg, #F1E4D0 0%, #D7BA9B 50%, #D0AF8A 100%);

		.no-data {
			color: #fff;
			font-size: 60rpx;
			font-weight: bold;
			text-align: center;
			padding-top: 100rpx;
			position: fixed;
			left: 0;
			width: 100%;
			z-index: 10;
			top: 300rpx;
		}

		.my-lottery-record {
			position: fixed;
			right: 0;
			top: 280rpx;
			padding: 6rpx 10rpx;
			font-family: 'PingFang SC-Semibold';
			font-size: 24rpx;
			text-align: center;
			border: 2rpx solid #986443;
			color: #986443;
			border-radius: 25rpx 0 0;
			background: linear-gradient(180deg, #D5AF77 0%, #DEBB80 6%, #DEBB80 94%, #D5AF77 100%);
			z-index: 1;
		}

		.lottery-name {
			display: flex;
			justify-content: center;
			margin-top: 50rpx;
			font-family: 'PingFang SC-Semibold';
			font-size: 42rpx;
			font-weight: 500;
			color: #986443;
		}

		.active-name {
			margin-top: 20rpx;
			display: flex;
			justify-content: center;
			color: #A5634C;
			font-family: 'PingFang SC-Medium';
			font-size: 28rpx;
			font-weight: 400;
		}

		.turntable {
			margin-top: 80rpx;
		}

		.active-rule {
			margin: 30rpx 20rpx 10rpx;

			.header {
				display: flex;
				justify-content: center;
				align-items: center;
				background-color: #D4AD76;
				height: 90rpx;
				border-radius: 20rpx 20rpx 0 0;
				font-family: 'PingFang SC-Semibold';
				font-size: 37rpx;
				font-weight: 500;
				color: #986443;
			}

			.body {
				/* 添加阴影效果 */
				box-shadow: 4rpx 4rpx 4rpx rgba(0, 0, 0, 0.4);

				display: flex;
				flex-direction: column;
				background-color: #F5DEB8;
				padding: 20rpx;


				.integral {
					display: flex;
					flex-direction: column;

					.title {
						margin: 0 15rpx;
						width: 50%;
						height: 50rpx;
						display: flex;
						align-items: center;
						border-radius: 25rpx 0 0 25rpx;
						border-right-color: transparent;

						.text {
							position: absolute;
							font-family: 'PingFang SC-Semibold';
							font-size: 24rpx;
							font-weight: 500;
							color: #986443;
							padding-left: 8rpx;
							width: 40%;
							border: 2rpx solid #986443;
							border-right-color: transparent;
							border-radius: 8rpx;
						}

						.num {
							font-family: 'PingFang SC-Semibold';
							font-size: 24rpx;
							font-weight: 500;
							text-align: center;
							border: 2rpx solid #986443;
							color: #986443;
							border-radius: 25rpx;
							margin-left: 10px;
							position: absolute;
							left: 190rpx;
							padding: 0 10rpx;
							width: 30%;
							background: linear-gradient(180deg, #D5AF77 0%, #DEBB80 6%, #DEBB80 94%, #D5AF77 100%);
						}

					}

					.content {
						font-family: 'PingFang SC-Medium';
						font-size: 20rpx;
						color: #986443;
						margin: 10rpx 0 0 10rpx;
					}
				}

				.time {
					margin-top: 20rpx;

					.title {
						font-family: 'PingFang SC-Semibold';
						font-size: 26rpx;
						font-weight: 500;
						color: #424141;
					}

					.start-time {
						// 样式定义
						font-family: 'PingFang SC-Medium';
						font-size: 24rpx;
						color: #986443;
						margin: 10rpx 0 0 10rpx;
					}

					.end-time {
						// 样式定义
						font-family: 'PingFang SC-Medium';
						font-size: 24rpx;
						color: #986443;
						margin: 10rpx 0 0 10rpx;
					}
				}

				.object-user {
					margin-top: 20rpx;

					.title {
						font-family: 'PingFang SC-Semibold';
						font-size: 26rpx;
						font-weight: 500;
						color: #424141;
					}

					.content {
						font-family: 'PingFang SC-Medium';
						font-size: 24rpx;
						color: #986443;
						margin: 10rpx 0 0 10rpx;
					}
				}

				.rule {
					margin-top: 20rpx;

					.title {
						font-family: 'PingFang SC-Semibold';
						font-size: 26rpx;
						font-weight: 500;
						color: #424141;
					}

					.content {
						// 样式定义
						// 多行文本样式可以使用 white-space: pre-line; 来保留换行符
						font-family: 'PingFang SC-Medium';
						font-size: 24rpx;
						color: #986443;
						white-space: pre-line;
						margin: 10rpx 0 0 10rpx;
					}
				}
			}
		}
	}

	.bottom-line {
		width: 100%;
		height: 50rpx;
		background-color: transparent;
	}
</style>